import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 

public class PlayAudio extends Activity {
 
    private static final String isPlaying = "Media is Playing";

    private MediaPlayer player;
    private Button birdButton;
    private Button frogButton;
    private Button catButton;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        // Get the button from the view
        birdButton = (Button) this.findViewById(R.id.bird);
        birdButton.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                playSound(1);
 
            }
        });
        frogButton = (Button) this.findViewById(R.id.frog);
        frogButton.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                playSound(2);
 
            }
        });
        catButton = (Button) this.findViewById(R.id.cat);
        catButton.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                playSound(3);
 
            }
        });
 
    }
 
    @Override
    public void onPause() {
        try{
        super.onPause();
        player.pause();
        }catch (Exception e){
 
        }
    }
 
    private void playSound(int arg){
        try{
            if (player.isPlaying()) {
                player.stop();
                player.release();
            }
        }catch(Exception e){
            Toast.makeText(this, " Masuk Exception", Toast.LENGTH_LONG).show();
        }
        if (arg == 1){
 
            Toast.makeText(this, isPlaying+" Bird", Toast.LENGTH_LONG).show();
            player = MediaPlayer.create(this, R.raw.bird);
 
        }else if (arg==2){
            Toast.makeText(this, isPlaying+" Frogs", Toast.LENGTH_LONG).show();
            player = MediaPlayer.create(this, R.raw.frogs);
        }else if (arg==3){
            Toast.makeText(this, isPlaying+" Cat", Toast.LENGTH_LONG).show();
            player = MediaPlayer.create(this, R.raw.cat);
        }
        player.setLooping(false); // Set looping
        player.start();
    }
 
}